<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      :rules="formRules"
      ref="queryFormRef"
      :inline="true"
      label-width="90px"
    >
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="平台订单号" clearable class="!w-240px"/>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table v-loading="loading" border :show-overflow-tooltip="true" :data="list"  style="width: 100%;">
      <el-table-column label="业务类型" align="center" prop="subject" width="150"/>
      <el-table-column label="病人姓名" align="center" prop="BRXM" width="90" />
      <el-table-column label="病人ID" align="center" prop="BRID" width="85" />
      <el-table-column label="平台订单号" align="center" prop="mchOrderNo" width="220" />
      <el-table-column label="支付订单号" align="center" prop="payOrderId" width="220" />
      <el-table-column label="渠道订单号" align="center" prop="channelOrderNo" width="220" />
      <el-table-column label="金额(元)" align="center" prop="amount" width="90" />
      <el-table-column label="支付时间" align="center" prop="successTime" width="170" />
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="95"
        label="操作">
        <template #default="scope">
          <el-button type="primary" plain size="small" v-if="scope.row.state === 2 " @click="refund(scope.row.BRXM,scope.row.mchOrderNo,scope.row.payOrderId,scope.row.amount)">退款</el-button>
          <span v-if="scope.row.refundState === 2" style="color: red"> 已退款 </span>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>
</template>

<script lang="ts" setup>
import * as RefundApi from "@/api/hospital/refund";

defineOptions({ name: 'QrcodepayRefund' })

const message = useMessage() // 消息弹窗

const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const queryParams = reactive({
  orderType: 'QAC',
  orderNo: null
})

const formRules = reactive({
  orderNo: [{required: true, message: '订单号不能为空', trigger: 'blur'}],
})


const queryFormRef = ref() // 搜索的表单


/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
}

/** 查询列表 */
const getList = async () => {
  await queryFormRef.value.validate()
  loading.value = true
  try {
    const data = await RefundApi.getOrderList(queryParams)
    console.table(data)
    list.value = data
  } finally {
    loading.value = false
  }
}

const refund  = async (orderInfo) => {
  try {
    await message.confirm(`确认给患者[${orderInfo.patientName}]进行退款[${orderInfo.cost}]元?`, '提示',)
    // 发起退款
    const refundParams = {
      orderType: queryParams.orderType,
      orderNo: orderInfo.orderNo,
      refundFee: orderInfo.cost
    }

    loading.value = true
    try {
      const data = await RefundApi.refundOrder(refundParams)
      message.success("操作成功")
      // 刷新列表
      await getList()
    } finally {
      loading.value = false
    }

  } catch {}
}

</script>

<style scoped>

</style>
